package com.hk.yajin.function.members.activity.setting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.yajin.R;
import com.hk.yajin.base.BaseActivity;
import com.hk.yajin.function.members.activity.AcountDistrictListActivity;
import com.hk.yajin.manager.ObtainValidateCode;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.url.UrlConstants;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgetPwdActivity extends BaseActivity {
    @BindView(R.id.tv_select_district)
    TextView tvSelectDistrict;
    @BindView(R.id.iv_select_district)
    ImageView ivSelectDistrict;
    @BindView(R.id.tv_district_num)
    TextView tvDistrictNum;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_send_code)
    TextView tvSendCode;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.lly_phone)
    LinearLayout llyPhone;
    private ObtainValidateCode obtainValidateCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.bind(this);
        obtainValidateCode = new ObtainValidateCode(handler);
        initToolBar(R.string.forget_pwd_title);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case ObtainValidateCode.HANDLER_VALIDATE_BUTTON_OF_INIT:
                    tvSendCode.setText(getString(R.string.register_sms_get));
                    tvSendCode.setClickable(true);

                    break;
                case ObtainValidateCode.HANDLER_UPDATE_VALIDATE_BUTTON:
                    int second = (int) msg.obj;
                    tvSendCode.setText("(" + second + "s)重新获取");
                    tvSendCode.setClickable(false);

                    break;
            }
        }
    };

    @OnClick({R.id.tv_send_code, R.id.tv_sure,R.id.tv_district_num,R.id.iv_select_district,R.id.tv_select_district})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_send_code:
                requestSmsCode();
                break;
            case R.id.tv_sure:
                submitSmsAndData();
                break;
            case R.id.iv_select_district:
            case R.id.tv_select_district:
            case R.id.tv_district_num:
                startActivityForResult(new Intent(this, AcountDistrictListActivity.class), Activity.RESULT_FIRST_USER);
                break;
        }
    }

    private void submitSmsAndData() {
        String acount = etPhone.getText().toString();
        String pwd = etPwd.getText().toString();
        String code = etCode.getText().toString();
        if (TextUtils.isEmpty(code)) {
            showShortToast(getString(R.string.please_input_code));
            return;
        }
        if (TextUtils.isEmpty(acount)) {
            showShortToast("账号不能为空");
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            showShortToast("密码不能为空");
            return;
        }

//        if (acount.length() < 11 || !acount.startsWith("1")) {
//            showShortToast("请输入有效的手机号");
//            return;
//        }

        if (pwd.length() < 6) {
            showShortToast("密码不能低于六位");
            return;
        }

        if (isNetWorkConnect()) {
            showLoadingView();
            Map<String, Object> reqParam = new HashMap<String, Object>();
            reqParam.put("mobile", acount);
            reqParam.put("password", pwd);
            reqParam.put("code", code);
            reqParam.put("mobile_area_code", tvDistrictNum.getText().toString());
            HttpRequestUtils.post(false, UrlConstants.FUNCTION_REST_PWD, reqParam, false, new HttpCallBack() {

                @Override
                public void onSucced(String data) {
                    hideLoadingView();
                    showShortToast("请用新密码登陆");
                    finish();
                }

                @Override
                public void onErrorCode(NewWorkBean bean) {
                    hideLoadingView();
                    showShortToast(bean.getMsg());
                }

                @Override
                public void onBadHttp(String tip) {
                    hideLoadingView();
                    showShortToast(tip);
                }

            });
        }
    }

    private void requestSmsCode() {
        String acount = etPhone.getText().toString();
        if (TextUtils.isEmpty(acount)) {
            showShortToast("账号不能为空");
            return;
        }

        if (acount.length() < 11 || !acount.startsWith("1")) {
            showShortToast("请输入有效的手机号");
            return;
        }

        if (isNetWorkConnect()) {
            Map<String, Object> reqParam = new HashMap<String, Object>();
            reqParam.put("mobile", acount);
            reqParam.put("type", "forget_password");
            HttpRequestUtils.post(false, UrlConstants.FUNCTION_GET_SMS_CODE, reqParam, false, new HttpCallBack() {

                @Override
                public void onSucced(String data) {
                    showShortToast("验证码已发送");
                    obtainValidateCode.hadSendCode = true;
                    obtainValidateCode.changeButtonUIAndGetCode(60);
                }

                @Override
                public void onErrorCode(NewWorkBean bean) {
                    showShortToast(bean.getMsg());
                }

                @Override
                public void onBadHttp(String tip) {
                    showShortToast(tip);
                }

            });
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Activity.RESULT_FIRST_USER && resultCode == Activity.RESULT_OK && data != null) {
            tvDistrictNum.setText(data.getStringExtra(AcountDistrictListActivity.DISTRICT_NUM));
            tvSelectDistrict.setText(data.getStringExtra(AcountDistrictListActivity.DISTRICT_NAME));
        }
    }
}
